<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends Controller{
	public function add(){
		if(IS_POST){
			$model = D('Category');
			if($model->create(I('post.'),1)){
				if($model->add()){
					$this->success('添加成功！',U('lst'));
					die;
				} 
			}
			$error = $model->getError();
			$this->error($error); 
		}
		
		$catModel = D('Category'); 
		$data = $catModel->getTree();
		
		$this->assign(array(
				'data'=>$data,
				'_page_title'=>'分类添加页',
				'_page_btn_name'=>'分类列表',
				'_page_btn_link'=>U('lst')
		));
		$this->display();
	}
	
	public function lst(){
		$model = D('Category');
		$data = $model->getTree();
// 		print_r($data);die;
		$this->assign('data',$data);
		
		$this->assign(array(
			'_page_title'=>'分类列表',
			'_page_btn_name'=>'添加分类',
			'_page_btn_link'=>U('add')
		));
		
		$this->display();
	}
	
	
	public function edit(){
		$id = I('get.id');
		if(IS_POST){
			$model = D('Category');
			if($model->create(I('post.'),2)){
				if($model->save() !== false){
					$this->success('修改成功！',U('lst'));
					die;
				}
			}
			$error = $model->getError();
			$this->error($error);
			die;
		}
		$model = D('Category');
		$info = $model->find($id);
		$data = $model->getTree();
		
		//取出所有子分类，过滤掉
		$children = $model->getChildren($id);
		$this->assign('children',$children);
		$this->assign('info',$info);
		$this->assign('data',$data);
		$this->assign(array(
				'_page_title'=>'分类修改页',
				'_page_btn_name'=>'分类列表',
				'_page_btn_link'=>U('lst')
		));
// 		dump($info);
		$this->display();
	}
	
	public function delete(){
		$id = I('get.id');
		$model = D('Category');
		if($model->delete($id) !== false){
			$this->success('删除成功！');
			die;
		}else{
			$this->error('删除失败');
			die;
		}
	}
	
	
}









